package com.example.day19;

import android.app.Activity;
import android.app.Application;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;

public class ActivityTools {

    public boolean checkAccess(String access, int code, Activity activity) {
        //Manifest.permission.READ_EXTERNAL_STORAGE
        //检查当前权限（若没有该权限，值为-1；若有该权限，值为0）
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int hasAccess = ContextCompat.checkSelfPermission(activity.getApplication(), access);
            Log.e("PERMISION_CODE", hasAccess + "***");
            if (hasAccess == PackageManager.PERMISSION_GRANTED) {
                // TODO: 2019/6/15 获得授权执行操作
//                music();
                Log.e("PERMISION_CODE", "6.0已授权");
                return true;
            } else {
                ActivityCompat.requestPermissions(activity, new String[]{access, "android.permission.READ_EXTERNAL_STORAGE"}, code);
                return false;
            }
        } else {
            // TODO: 2019/6/15 非6.0直接使用
            Log.e("PERMISION_CODE", "非6.0***");
//            music();
            return true;
        }
    }
}
